package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.domain.Permission;
import cn.wolfcode.crm.domain.Role;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IPermissionService;
import cn.wolfcode.crm.service.IRoleService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.PageResult;
import cn.wolfcode.crm.util.PermissionName;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("permission")
public class PermissionController {

    @Autowired
    private IPermissionService service;

    @RequestMapping("")
    @PermissionName("权限列表")
    @RequiresPermissions("permission:list")
    public String index(){
        return "permission/list";
    }

    @RequestMapping("list")
    @ResponseBody
    @PermissionName("权限数据")
    @RequiresPermissions("permission:data")
    public PageResult list(QueryObject qo){
        return service.query(qo);
    }

    @RequestMapping("load")
    @ResponseBody
    @PermissionName("权限加载")
    @RequiresPermissions("permission:load")
    public JsonResult save(){
        JsonResult result = new JsonResult();
        try {
            service.load();
        }catch(Exception e){
            e.printStackTrace();
            result = new JsonResult("加载权限失败");
        }
        return result;
    }
    @RequestMapping("queryPermissionByRoleId")
    @ResponseBody
    public List<Permission> queryPermissionByRoleId(Long roleId){
        return service.queryPermissionByRoleId(roleId);
    }
}
